package com.jcfy.project.module.client;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jcfy.common.utils.poi.ExcelUtil;
import com.jcfy.framework.aspectj.lang.annotation.Log;
import com.jcfy.framework.aspectj.lang.enums.BusinessType;
import com.jcfy.framework.base.Error;
import com.jcfy.framework.web.controller.BaseController;
import com.jcfy.framework.web.domain.AjaxResult;
import com.jcfy.framework.web.page.TableDataInfo;
import com.jcfy.project.module.domain.TGoods;
import com.jcfy.project.module.domain.TTeam;
import com.jcfy.project.module.service.ITGoodsService;
import com.jcfy.project.module.service.ITTeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品Controller
 *
 * @author hushuai
 * @date 2020-08-16
 */
@RestController
@RequestMapping("/client/goods")
public class GoodsController extends BaseController {
    @Autowired
    private ITGoodsService tGoodsService;
    @Autowired
    private ITTeamService teamService;

    /**
     * 查询首页推荐商品
     */
    @PostMapping("/home" )
    public TableDataInfo home(TGoods tGoods) {
        startPage();
        List<TGoods> list = tGoodsService.selectTGoodsList(tGoods);
        return getDataTable(list);
    }

    /**
     * 查询商品列表
     */
    @PostMapping("/list")
    public AjaxResult list(TGoods tGoods, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<TGoods> list = tGoodsService.selectTGoodsList(tGoods);
        PageInfo<TGoods> pageInfo = new PageInfo<TGoods>(list);
        return AjaxResult.success(pageInfo);
    }

    /**
     * 获取商品详细信息
     */
    @PostMapping(value = "/getInfo")
    public AjaxResult getInfo(Long goodsId) {
        try {
            TGoods goods = tGoodsService.selectTGoodsById(goodsId);
            TTeam team = teamService.selectTTeamById(goods.getTeamId());
            Map<String, Object> data = new HashMap<>();
            data.put("goods",goods);
            data.put("team",team);
            return AjaxResult.success(data);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(Error._100000, e.getMessage());
        }
    }

    /**
     * 导出商品列表
     */
    @PreAuthorize("@ss.hasPermi('module:goods:export')")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TGoods tGoods) {
        List<TGoods> list = tGoodsService.selectTGoodsList(tGoods);
        ExcelUtil<TGoods> util = new ExcelUtil<TGoods>(TGoods.class);
        return util.exportExcel(list, "goods");
    }

    /**
     * 新增商品
     */
    @PreAuthorize("@ss.hasPermi('module:goods:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TGoods tGoods) {
        return toAjax(tGoodsService.insertTGoods(tGoods));
    }

    /**
     * 修改商品
     */
    @PreAuthorize("@ss.hasPermi('module:goods:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TGoods tGoods) {
        return toAjax(tGoodsService.updateTGoods(tGoods));
    }

    /**
     * 删除商品
     */
    @PreAuthorize("@ss.hasPermi('module:goods:remove')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{goodsIds}")
    public AjaxResult remove(@PathVariable Long[] goodsIds) {
        return toAjax(tGoodsService.deleteTGoodsByIds(goodsIds));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('module:goods:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping("/recommendGoods")
    public AjaxResult recommendGoods(@RequestBody TGoods tGoods) {
        return toAjax(tGoodsService.updateTGoods(tGoods));
    }
}
